package top.auntie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.auntie.bean.PageReq;
import top.auntie.dao.OrgDao;
import top.auntie.model.Org;
import top.auntie.model.OrgExample;
import top.auntie.service.OrgService;
import top.auntie.util.DateTimeUtils;

import java.util.List;

@Service
public class OrgServiceImpl implements OrgService {

    @Autowired
    private OrgDao orgDao;

    @Override
    public PageInfo queryOrg(PageReq pageReq) {
        PageHelper.startPage(pageReq.getPage(), pageReq.getLimit(), true);
        return new PageInfo<>(orgDao.selectByExample(new OrgExample()));
    }

    @Override
    public void saveOrg(Org org) {
        if (org.getId()!=null) {
            org.setUpdatedAt(DateTimeUtils.getNowDateTimeStr());
            orgDao.updateByPrimaryKeySelective(org);
        } else {
            org.setCreatedAt(DateTimeUtils.getNowDateTimeStr());
            orgDao.insertSelective(org);
        }
    }

    @Override
    public void deleteOrg(Integer id) {
        orgDao.deleteByPrimaryKey(id);
    }

    @Override
    public Org getOrgById(Integer id) {
        return orgDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Org> getAllOrg() {
        return orgDao.selectByExample(new OrgExample());
    }

}
